<template>
  <view class="content">
    <view class="without-login">
      <view class="iconv u-flex-col " style="margin-bottom: 140rpx;">
      </view>
      <button round type="primary" open-type="getUserInfo" :disabled="authLoading" @getuserinfo="getUserInfo">{{ share ? '微信号快速注册' : '微信号快速登录' }}</button>
      <view @click="naviHome" class="no-login">{{ share ? '暂不注册' : '暂不登录' }}</view>
    </view>
    <uni-popup
        ref="popup"
        type="bottom"
        class="popup-height"
        background-color="#fff"
    >
      <view class="register-popup">
        <view class="tips">
          温馨提示
        </view>
        <view class="allow-wrap">
          <view class="no-register">
            检测到您还未注册账号
          </view>
          <view class="allow">
            为了方便我们与您联系，请允许我们获取您的手机号码
          </view>
        </view>
        <view class="btn-wrap">
          <button class="phone-btn" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber" type="primary">授权电话号码</button>
        </view>
      </view>
    </uni-popup>
  </view>
</template>

<script>
export default {
  name: 'Login',
  components: {
  },
  data() {
    return {
      authLoading: false,
      showComplete: false,
      hbid: '',
      redirect: '',
      type: '',
      share: false
    }
  },
  onLoad(op) {
    console.log(op)
    if (op.uid && op.share === 'ok') {
      this.hbid = op.uid
      this.share = true
    }
    if (op.type) {
      this.type = op.type
    }
    // let pages = getCurrentPages()
    // const preRoute = pages[pages.length - 2]
    // if (preRoute) {
    //   if (preRoute.route === 'pages/home/index') {
    //     this.redirect = ''
    //   }
    //   this.redirect = preRoute.$page.fullPath
    // }
  },
  methods: {
    open() {
      this.$refs.popup.open('top')
    },
    close() {
      this.$refs.popup.close()
    },
    naviHome() {
      uni.navigateTo({
        url: '/pages/home/index'
      })
    },
    getUserInfo(e) {
      this.authLoading = true
      console.log(e.userInfo || e.target.userInfo)
      this.$store.commit('$uStore', { name: 'vx_t_u_info', value: e.userInfo || e.target.userInfo })
      uni.login({
        provider: 'weixin',
        success: res => {
          if (res.code) {
            this.$store.dispatch('$dLogin', res.code).then(res => {
              if (res.type === false) {
                this.$refs.popup.open('bottom')
              } else if (res.type === true) {
                uni.reLaunch({ url: '/pages/user-center/index' })
              }
            })
          }
        }
      })
    },
    async getPhoneNumber(e) {
      let obj = e.detail || e
      if (this.hbid) {
        obj.hbid = this.hbid
      }
      if (this.type) {
        obj.type = this.type
      }
      if (obj.errMsg === 'getPhoneNumber:ok') {
        this.$store.dispatch('$dGetPhone', obj).then(res => {
          uni.navigateTo({ url: `/pages/register/index?data=${JSON.stringify(res)}` })
        })
      } else {
        uni.showToast({ title: '您拒绝了授权~,注册失败~' })
      }
    },
    onClose() {
      this.showComplete = false
      this.authLoading = false
    }
  }
}
</script>

<style lang="scss" scoped>
.without-login {
  width: 100vw;
  height: 100vh;
  background-color: #fff;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
.no-login {
  margin-top: 40rpx;
  font-size: 33rpx;
}
.popup-height {
  height: 40vh;
}
.register-popup {
  height: 40vh;
  padding: 30rpx;
  .tips {
    font-size: 36rpx;
    font-weight: bold;
    text-align: center;
  }
  .btn-wrap {
    margin: 30rpx auto;
    display: flex;
    justify-content: center;
  }
  .phone-btn {
    margin-top: 50rpx;
  }
  .no-register {
    font-size: 28rpx;
    color: #3b4144;
  }
  .allow-wrap {
    margin-top: 60rpx;
    text-align: center;
  }
  .allow {
    font-size: 28rpx;
    margin-top: 20rpx;
    color: #909399;
  }
}
</style>
